/*
 * Copyright (C) 2003-2010 The Music Player Daemon Project
 * http://www.musicpd.org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "config.h"
#include "decoder_api.h"
#include "decoder_control.h"
#include "decoder_internal.h"
#include "audio_check.h"
#include "path.h"
#include "utils.h"

#include "despotify.h"
#include "spotify/spotify.h"
#include <glib.h>

#include <assert.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

void spotify_streamdecode(struct decoder *decoder, struct input_stream *is)
{
struct decoder_control *dc = decoder->dc;
	struct audio_format audio_format;
	struct pcm_data pcm, old_pcm;
	int size, cmd;
	do {
		size = decoder_read(decoder, is, &pcm, sizeof(struct pcm_data));
		if(size > 0 && pcm.samplerate != 0 && old_pcm.samplerate != pcm.samplerate || !audio_format_valid(&audio_format))
		{
			g_warning("sample_rate=%d, ",  pcm.samplerate);
			g_warning("valid_channel=%d, ",  pcm.channels);		
			old_pcm.samplerate = pcm.samplerate;
			audio_format.sample_rate = pcm.samplerate;
			audio_format.channels = pcm.channels;
			audio_format.reverse_endian = 0;
			audio_format.format = SAMPLE_FORMAT_S16;
			if(audio_format_valid(&audio_format))
			{
				g_warning("test");
				decoder_initialized(decoder, &audio_format, is->seekable, -1);
			}
		}
		if(size > 0 && pcm.samplerate != 0 && audio_format_valid(&audio_format))
		{
			cmd = decoder_data(decoder, is, pcm.buf, pcm.len, /*pcm.samplerate*/320);
		}
	}
	while (cmd != DECODE_COMMAND_STOP && !input_stream_eof(is));		
}

struct tag * spotify_tag(struct input_stream *is)
{
g_warning("tag");
	char t[10];
	struct tag * tag;
	struct link* link = despotify_link_from_uri(is->uri);
	struct track* track = despotify_link_get_track(despotify_session, link);
	tag = tag_new();
	tag_begin_add(tag);
	tag_add_item_n(tag, TAG_ARTIST, track->artist->name, strlen(track->artist->name));
	tag_add_item_n(tag, TAG_TITLE, track->title, strlen(track->title));
	tag_add_item_n(tag, TAG_TRACK, t, strlen(t));	
	tag_add_item_n(tag, TAG_ALBUM, track->album, strlen(track->album));
	tag_end_add(tag);
	tag->time = track->length/1000;	
	return tag;
}

static char const *const spotify_suffixes[] = {
	NULL
};

static char const *const spotify_mime_types[] = {
	"audio/spotify",
	NULL
};

const struct decoder_plugin spotify_decoder_plugin = {
	.name = "spotify",
	.stream_decode = spotify_streamdecode,
	.stream_tag = spotify_tag,
	.suffixes = spotify_suffixes,
	.mime_types = spotify_mime_types
};
